<template>
  <view class="cart-contanier" v-if="cart.length!=0">
    <!-- 收货地址组件 -->
    <my-address></my-address>
    <!-- 购物车标题 -->
    <view class="cart-title">
      <!-- 左侧图标 -->
      <uni-icons type="shop" size="22"></uni-icons>
      <!-- 右侧文字 -->
      <text class="cart-title-text">购物车</text>
      <text class="cart-title-count" v-if="cart.length>0">（{{cart.length}}）</text>
    </view>

    <!-- 循环渲染商品信息 -->
    <!-- <block v-for="(goods,index) in cart" :key="index">
      <my-goods ref="myGoods" :goods="goods" :show-radio="true" :show-num="true" @go-detail="gotoDetail(goods)" @radio-change="radioChange" @num-change="numberChange"></my-goods>
    </block> -->
    <uni-swipe-action>
      <block v-for="(goods,index) in cart" :key="index">
        <uni-swipe-action-item :right-options="options" @click="swipeActionClick(goods)">
          <my-goods ref="myGoods" :goods="goods" :show-radio="true" :show-num="true" @go-detail="gotoDetail(goods)"
            @radio-change="radioChange" @num-change="numberChange"></my-goods>
        </uni-swipe-action-item>
      </block>
    </uni-swipe-action>
    
    <!-- 使用自定义结算组件 -->
    <my-settle></my-settle>
  </view>
  
  <!-- 空白购物车 -->
  <view class="empty-cart" v-else>
    <image src="/static/cart_empty@2x.png" class="empty-img"></image>
    <text class="tip-text">空空如也</text>
  </view>
</template>

<script>
  import badgeMin from '@/mixins/tabbar-badge.js'
  import {
    mapState,
    mapMutations
  } from 'vuex'
  export default {
    mixins: [badgeMin],
    computed: {
      ...mapState('m_cart', ['cart'])
    },
    data() {
      return {
        options: [{
             text: '删除', // 显示的文本内容
             style: {
               backgroundColor: '#C00000' // 按钮的背景颜色
             }
           }]
      }
    },
    onShow(){
      this.gotoHome()
    },
    methods: {
      //映射购物车勾选updateGoodsStatus方法
      ...mapMutations('m_cart', ['updateGoodsStatus', 'updateGoodsCount','removeGoodsById']),
      gotoHome(){
        if(this.cart.length === 0){
          uni.$showMsg('请前往首页购买商品')
          setTimeout(()=>{
            uni.switchTab({
              url:'/pages/home/home'
            })
          },2000)
        }
      },
      gotoDetail(goods) {
        // 跳转到详情页
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id=' + goods.goods_id
        })
      },
      // 购物车单选按钮
      radioChange(e) {
        // console.log(e);
        this.updateGoodsStatus(e)
      },
      // 商品数量变化
      numberChange(e) {
        this.updateGoodsCount(e)
        // if(e.goods_count === 0){
        //    uni.$showMsg('宝贝数量不能再减少了')
        // }
      },
      // 点击删除滑块
      swipeActionClick(goods){
        this.removeGoodsById(goods.goods_id)
        if(this.total >0){
          this.setBadge()
        }else{
          this.hideBadge()
        }
        this.gotoHome()
      }
    },
    onPullDownRefresh() {
      this.$refs.myGoods.forEach(item => {
        item.showNumBox = false
      })
      uni.stopPullDownRefresh()
    },
    onReachBottom(){
      this.$refs.myGoods.forEach(item => {
        item.showNumBox = false
      })
    }
  }
</script>

<style lang="scss">
  .cart-contanier{
    padding-bottom: 50px;
  }
  .cart-title {
    height: 40px;
    display: flex;
    align-items: center;
    padding-left: 5px;
    border-bottom: 1px solid #efefef;

    .cart-title-text {
      font-size: 18px;
      margin-left: 10px;
    }

    .cart-title-count {
      font-size: 16px;
      margin-left: -6px;
    }
  }
  .empty-cart{
    display: flex;
    flex-direction: column;
    align-items: center;
    padding-top: 150px;
    .empty-img{
      width: 90px;
      height: 90px;
    }
    .tip-text{
      font-size: 12px;
      color: gray;
      margin-top: 15px;
    }
  }
</style>
